package com.abcolive.software.phone.android;

import android.app.Activity;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Slider extends Activity implements OnClickListener {	
    private Button yes;
    private Button no;
    private Button unknow;
    
    private HorizontalScrollView slider;
    //private LinearLayout frameSelected;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        /* Get Display properties */
        Display display = getWindowManager().getDefaultDisplay();       
        
        //Form Items
        FormItem[] items = {
        	new FormItem("Coniferous", R.drawable.coniferous_trees, R.string.treeDescriptionConiferous, R.string.treeHelpTextConiferous),
        	new FormItem("Fruit", R.drawable.fruit_bearing_trees, R.string.treeDescriptionFruit, R.string.treeHelpTextFruit),
        	new FormItem("Nut", R.drawable.nut_bearing_trees, R.string.treeDescriptionNut, R.string.treeHelpTextNut),
        	new FormItem("Seed", R.drawable.seed_bearing_trees, R.string.treeDescriptionSeed, R.string.treeHelpTextSeed),
        	new FormItem("Tiny", R.drawable.tiny_seed_bearing_trees, R.string.treeDescriptionTiny, R.string.treeHelpTextTiny),
        	new FormItem("Other", R.drawable.help_128, R.string.treeDescriptionOther, R.string.treeHelpTextOther)
        };
                        
        //The Whole View
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        
        //Title
        TextView textTitle = new TextView(this);
        textTitle.setText("Trees");
        textTitle.setTextSize(24);
        textTitle.setGravity(Gravity.CENTER);
        textTitle.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        layout.addView(textTitle);
        
        //The Slider Container
        slider = new HorizontalScrollView(this);
        slider.setScrollBarStyle(HorizontalScrollView.SCROLLBARS_INSIDE_INSET);
        //slider.setFillViewport(isChild());
        //slider.getFocusedRect(new Rect(10,10,display.getWidth()-10,display.getHeight()-10));        
        
        //The Images Container
        LinearLayout imagesContainer = new LinearLayout(this);
        imagesContainer.setOrientation(LinearLayout.HORIZONTAL);
        
        //For each image        
        for (int i = 0; i < items.length; i++) {
        	/* Frame */        	
        	LinearLayout frame = new LinearLayout(this);
        	frame.setPadding(5, 5, 5, 5);
        	frame.setOrientation(LinearLayout.VERTICAL);
        	frame.setFocusable(true);
        
        	/* Image */        	
        	ImageView image = new ImageView(this);
        	image.setImageResource(items[i].getResource());
        	image.setAdjustViewBounds(true);
        	image.setScaleType(ScaleType.CENTER);
        	image.setMinimumWidth(display.getWidth());
        	image.setMaxHeight(display.getHeight()/2);
        	image.setTag("FrameImage");
        	    	
        	/* Text */
        	TextView text = new TextView(this);        	
        	text.setText("Image "+i);
        	text.setGravity(Gravity.CENTER);
        	text.setTag("FrameText");
        	
        	frame.addView(image);
        	frame.addView(text);
        	imagesContainer.addView(frame);
        }        
        slider.addView(imagesContainer);
        layout.addView(slider);
        
        /* Button Choices */
        LinearLayout buttonsContainer = new LinearLayout(this);
        buttonsContainer.setOrientation(LinearLayout.HORIZONTAL);
        buttonsContainer.setGravity(Gravity.CENTER);
        
        yes = new Button(this);
        yes.setGravity(Gravity.CENTER);
        yes.setText(R.string.treeYes);
        yes.setOnClickListener(this);
        buttonsContainer.addView(yes);

    	no = new Button(this);
        no.setGravity(Gravity.CENTER);
        no.setText(R.string.treeNo);
        no.setOnClickListener(this);
        buttonsContainer.addView(no);

    	unknow = new Button(this);
        unknow.setGravity(Gravity.CENTER);
        unknow.setText(R.string.treeUnknow);
        unknow.setOnClickListener(this);
        buttonsContainer.addView(unknow);
        
        layout.addView(buttonsContainer);
        
        setContentView(layout);        
    }
    
	@Override
	public void onClick(View v) {
		LinearLayout selected = (LinearLayout) slider.findFocus();
		TextView selectedText = (TextView) selected.findViewWithTag("FrameText");
		if (selectedText == null) {
			selectedText = new TextView(this);
			selectedText.setText("Nothing");
		}
		
		if (v == yes) {		
			Toast.makeText(getApplicationContext(), "You choose "+yes.getText()+" for "+selectedText.getText(), Toast.LENGTH_SHORT).show();
		} else if ( v == no ) {
			Toast.makeText(getApplicationContext(), "You choose "+no.getText()+" for "+selectedText.getText(), Toast.LENGTH_SHORT).show();
		} else if ( v == unknow ) {
			Toast.makeText(getApplicationContext(), "You choose "+unknow.getText()+" for "+selectedText.getText(), Toast.LENGTH_SHORT).show();
		}
	}
}